home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / full / jbuild / setup / JBuilder / jsamples.z / juggler.jar / sunw / demo / juggler / Juggler.class (.txt) next >
Encoding:
Java Class File  |  1997-06-04  |  3.2 KB  |  157 lines

  1. package sunw.demo.juggler;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.event.ActionEvent;
  9. import java.net.URL;
  10.  
  11. public class Juggler extends Applet implements Runnable {
  12.    private transient Image[] images = new Image[5];
  13.    private transient Thread animationThread;
  14.    private int rate = 125;
  15.    private transient int loop;
  16.    private boolean stopped = true;
  17.  
  18.    public synchronized void start() {
  19.       this.startJuggling();
  20.    }
  21.  
  22.    public synchronized void stop() {
  23.       this.stopJuggling();
  24.    }
  25.  
  26.    public void init() {
  27.       this.images = new Image[5];
  28.  
  29.       for(int var1 = 0; var1 < 5; ++var1) {
  30.          String var2 = "sunw/demo/juggler/Juggler" + var1 + ".gif";
  31.          this.images[var1] = this.loadImage(var2);
  32.          if (this.images[var1] == null) {
  33.             System.err.println("Couldn't load image " + var2);
  34.             return;
  35.          }
  36.       }
  37.  
  38.    }
  39.  
  40.    private Image loadImage(String var1) {
  41.       try {
  42.          URL var2 = new URL(((Applet)this).getCodeBase(), var1);
  43.          Image var3 = ((Applet)this).getImage(var2);
  44.          return var3;
  45.       } catch (Exception var4) {
  46.          return null;
  47.       }
  48.    }
  49.  
  50.    public void paint(Graphics var1) {
  51.       int var2 = this.loop % 4 + 1;
  52.       if (this.stopped) {
  53.          var2 = 0;
  54.       }
  55.  
  56.       if (this.images != null && var2 < this.images.length) {
  57.          Image var3 = this.images[var2];
  58.          if (var3 != null) {
  59.             var1.drawImage(var3, 0, 0, this);
  60.          }
  61.  
  62.       }
  63.    }
  64.  
  65.    public synchronized void setEnabled(boolean var1) {
  66.       super.setEnabled(var1);
  67.       this.notify();
  68.    }
  69.  
  70.    public synchronized void startJuggling() {
  71.       if (this.images == null) {
  72.          this.init();
  73.       }
  74.  
  75.       if (this.animationThread == null) {
  76.          this.animationThread = new Thread(this);
  77.          this.animationThread.start();
  78.       }
  79.  
  80.       this.stopped = false;
  81.       this.notify();
  82.    }
  83.  
  84.    public synchronized void stopJuggling() {
  85.       this.stopped = true;
  86.       this.loop = 0;
  87.       Graphics var1 = ((Component)this).getGraphics();
  88.       if (var1 != null && this.images != null) {
  89.          Image var2 = this.images[0];
  90.          if (var2 != null) {
  91.             var1.drawImage(var2, 0, 0, this);
  92.          }
  93.  
  94.       }
  95.    }
  96.  
  97.    public void startJuggling(ActionEvent var1) {
  98.       this.startJuggling();
  99.    }
  100.  
  101.    public void stopJuggling(ActionEvent var1) {
  102.       this.stopJuggling();
  103.    }
  104.  
  105.    public int getAnimationRate() {
  106.       return this.rate;
  107.    }
  108.  
  109.    public void setAnimationRate(int var1) {
  110.       this.rate = var1;
  111.    }
  112.  
  113.    public Dimension getMinimumSize() {
  114.       return new Dimension(144, 125);
  115.    }
  116.  
  117.    /** @deprecated */
  118.    public Dimension minimumSize() {
  119.       return this.getMinimumSize();
  120.    }
  121.  
  122.    public Dimension getPreferredSize() {
  123.       return this.minimumSize();
  124.    }
  125.  
  126.    /** @deprecated */
  127.    public Dimension preferredSize() {
  128.       return this.getPreferredSize();
  129.    }
  130.  
  131.    public void run() {
  132.       try {
  133.          while(true) {
  134.             synchronized(this){}
  135.  
  136.             try {
  137.                while(this.stopped || !((Component)this).isEnabled()) {
  138.                   this.wait();
  139.                }
  140.             } catch (Throwable var5) {
  141.                throw var5;
  142.             }
  143.  
  144.             ++this.loop;
  145.             Graphics var1 = ((Component)this).getGraphics();
  146.             Image var2 = this.images[this.loop % 4 + 1];
  147.             if (var1 != null && var2 != null) {
  148.                var1.drawImage(var2, 0, 0, this);
  149.             }
  150.  
  151.             Thread.sleep((long)this.rate);
  152.          }
  153.       } catch (InterruptedException var6) {
  154.       }
  155.    }
  156. }
  157.